﻿Imports Microsoft.Office.Interop.Outlook

Public Class pmToolbar

    Public _app As Outlook.Application
    Private _disposed As Boolean
    Private _cbBar As Office.CommandBar

    Private WithEvents _cbMoveButton As Office.CommandBarButton
    Private WithEvents _cbUndoButton As Office.CommandBarButton
    Private WithEvents _cbSearchButton As Office.CommandBarButton
    Private WithEvents _cbExcelButton As Office.CommandBarButton
    Private WithEvents _cbFollowUpButton As Office.CommandBarButton
    Private WithEvents _cbRemindersButton As Office.CommandBarButton


    Friend Sub New(ByVal App As Outlook.Application)

        ' Store a reference to the Outlook.Application object.
        _app = App

        ' Create a WYWO command bar button to show the While You Were Out dialog box.
        InitializeUI()

    End Sub

    Private Sub InitializeUI()
        ' Get a reference to the active Explorer object.
        Dim explorer As Outlook.Explorer = _app.ActiveExplorer

        ' Create a new (temporary) CommandBar object.
        _cbBar = explorer.CommandBars.Add("PMToolbar", Office.MsoBarPosition.msoBarTop, False, True)

        ' Create movebutton
        _cbMoveButton = CType(_cbBar.Controls.Add(Office.MsoControlType.msoControlButton), Office.CommandBarButton)
        With _cbMoveButton
            .Style = Office.MsoButtonStyle.msoButtonIconAndCaption
            .Caption = "Naar map verplaatsen"
            .FaceId = 1676
        End With

        ' Create undobutton
        _cbUndoButton = CType(_cbBar.Controls.Add(Office.MsoControlType.msoControlButton), Office.CommandBarButton)
        With _cbUndoButton
            .Style = Office.MsoButtonStyle.msoButtonIconAndCaption
            .Caption = "Verplaatsen ongedaan maken"
            .FaceId = 128
        End With

        ' Create searchbutton
        _cbSearchButton = CType(_cbBar.Controls.Add(Office.MsoControlType.msoControlButton), Office.CommandBarButton)
        With _cbSearchButton
            .Style = Office.MsoButtonStyle.msoButtonIconAndCaption
            .Caption = "Zoek klant/order"
            .FaceId = 1744
            .BeginGroup = True
        End With

        ' Create excelbutton
        _cbExcelButton = CType(_cbBar.Controls.Add(Office.MsoControlType.msoControlButton), Office.CommandBarButton)
        With _cbExcelButton
            .Style = Office.MsoButtonStyle.msoButtonIconAndCaption
            .Caption = "Infofiche"
            .FaceId = 263
            .BeginGroup = True
        End With

        ' Create followUpButton
        _cbFollowUpButton = CType(_cbBar.Controls.Add(Office.MsoControlType.msoControlButton), Office.CommandBarButton)
        With _cbFollowUpButton
            .Style = Office.MsoButtonStyle.msoButtonIconAndCaption
            .Caption = "Reactie binnen 48 uur"
            .FaceId = 1678
        End With

        ' Create RemindersButton
        _cbRemindersButton = CType(_cbBar.Controls.Add(Office.MsoControlType.msoControlButton), Office.CommandBarButton)
        With _cbRemindersButton
            .Style = Office.MsoButtonStyle.msoButtonIconAndCaption
            .Caption = "Venster herinneringen"
            .FaceId = 608
            .BeginGroup = True
        End With

        ' Make the new CommandBar visible.
        _cbBar.Visible = True

        explorer = Nothing

    End Sub

    Private Sub _cbMoveButton_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles _cbMoveButton.Click
        Try
            Using _pmMoveMailForm As pmMoveMailForm = New pmMoveMailForm(Me._app, GetSelectedMail)
                _pmMoveMailForm.ShowDialog()
            End Using
        Catch ex As System.Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub _cbRemindersButton_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles _cbRemindersButton.Click
        ' Create a new Appointment item and display it to the user.
        Dim outlookApp As Outlook.Application = DirectCast(Ctrl.Application, Outlook.Application)

        Dim newAppt As Outlook.AppointmentItem = DirectCast(outlookApp.CreateItem(Outlook.OlItemType.olAppointmentItem), Outlook.AppointmentItem)
        newAppt.Display(False)
    End Sub


    Private Function GetSelectedMail() As Outlook.MailItem
        Try
            If IsNothing(_app.ActiveExplorer.Selection) OrElse _app.ActiveExplorer.Selection.Count <> 1 Then
                Return Nothing
            End If

            Dim selectedObject As Object = _app.ActiveExplorer.Selection.Item(1)

            If (TypeOf selectedObject Is Outlook.MailItem) Then
                Return DirectCast(selectedObject, mailitem)
            End If

            Return Nothing
        Catch ex As System.Exception
            Return Nothing
        End Try
    End Function

    Private Sub _cbFollowUpButton_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles _cbFollowUpButton.Click
        Dim selectedMail As Outlook.MailItem = GetSelectedMail()

        If Not IsNothing(selectedMail) Then
            selectedMail.FlagDueBy = DateAdd(DateInterval.Day, 2, selectedMail.ReceivedTime)
            selectedMail.FlagIcon = Outlook.OlFlagIcon.olRedFlagIcon
            selectedMail.Save()
        End If
    End Sub

    Private Sub _cbExcelButton_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles _cbExcelButton.Click
        Dim excel As Microsoft.Office.Interop.Excel.Application

        Dim wb As Microsoft.Office.Interop.Excel.Workbook
        Try
            excel = New Microsoft.Office.Interop.Excel.Application

            wb = excel.Workbooks.Open(System.AppDomain.CurrentDomain.BaseDirectory.Replace("PMOutlookAddIn\bin\Debug\", "") & "Files\Distripost_Info_Fiche.xls")
            excel.Visible = True
            wb.Activate()
        Catch ex As System.Exception
            MessageBox.Show("Fout: " & ex.ToString)
        End Try

    End Sub
End Class
